



function useValidationType(){
    Ext.apply(Ext.form.VTypes, {
		phone		: /^(\d{3}[-]?){1,2}(\d{10})$/,  
		phoneText	: 'Not a valid phone number.  Must be in the format YYY-XXXXXXXXXX (ISD-PhoneNumber(10-digit))',  
		phoneMask	: /[\d-]/  
    });
};

function doFormReadOnly(form, compIds, doReadOnly){
	for(var a=0; a<compIds.length; a++){
		if(form.getForm().findField(compIds[a]) != null){
			form.getForm().findField(compIds[a]).setReadOnly(doReadOnly);
		}
	}
};

function validateFormOnLoad(form, compIds){
	useValidationType();
	var msg = null;
	for(var a=0; a<compIds.length; a++){
		var field = form.getForm().findField(compIds[a]);    
		if(field.getXType() == 'textfield'){
			
			if(field.getValue() == null || field.getValue() == ''){
				field.allowBlank = false;
				//field.msgTarget = 'under';
				var fieldId = compIds[a];
				if(fieldId.substring(fieldId.length-5, fieldId.length) == 'email' ||
				   fieldId.substring(fieldId.length-5, fieldId.length) == 'Email' ||
				   fieldId.substring(fieldId.length-5, fieldId.length) == 'EMAIL'){
				   field.vtype = 'email';
				   field.msgTarget = 'under';
				   field.markInvalid('Required field! Example: user@example.com');
				}
				if(fieldId.substring(0, 5) == 'phone'){
					field.vtype = 'phone';
					field.msgTarget = 'under';
				}
			}
		}
		if(field.getXType() == 'combo'){
			if(field.getValue() == null || field.getValue() == 'SELECT' || field.getValue() == ''){
				field.allowBlank = false;
				//field.msgTarget = 'under';
			}
		}		
		if(field.getXType() == 'checkbox'){
			if(field.getValue() == null || field.getValue() == ''){
				field.allowBlank = false;
				//field.msgTarget = 'under';
			}
		}
	}
};

function simpleFormValidation(form, compIds){
	useValidationType();
	
	var msg = null;
	for(var a=0; a<compIds.length; a++){
		var field = form.getForm().findField(compIds[a]);    
		if(field.getXType() == 'textfield'){
			if(field.getValue() == null || field.getValue() == ''){
				var fieldId = compIds[a];
				if(fieldId.substring(fieldId.length-5, fieldId.length) == 'email' ||
				   fieldId.substring(fieldId.length-5, fieldId.length) == 'Email' ||
				   fieldId.substring(fieldId.length-5, fieldId.length) == 'EMAIL'){
				   field.vtype = 'email';
				   field.markInvalid('Required field! Example: user@example.com');
				   //field.vtypeText = '';
				}
				if(fieldId.substring(0, 5) == 'phone'){
					field.vtype = 'phone';
				}
				else{
					field.markInvalid('Required field');					
				}
				msg = 'Please fill the form carefully!<br>You must take care of (<font color=red>*</font>) marked field.';
			}
		}
		if(field.getXType() == 'combo'){
			if(field.getValue() == null || field.getValue() == 'SELECT' || field.getValue() == ''){
				field.markInvalid('Must select an option');
				msg = 'Please fill the form carefully!<br>You must take care of (<font color=red>*</font>) marked field.';
			}
		}		
		if(field.getXType() == 'checkbox'){
			if(field.getValue() == null || field.getValue() == ''){
				field.markInvalid('Must check this option');
				msg = 'Please fill the form carefully!<br>You must take care of (<font color=red>*</font>) marked field.';
			}
		}
	}
	if(msg != null){
		return Ext.MessageBox.show({
	            title	: 'Validation',
 	            msg		: msg,
 	            autoWidth	: true,
 	            buttons	: Ext.MessageBox.OK,
 	            multiline: false
 	           }); 	
	}else{
		return 'true';
	}
	form.doLayout(true, true);
};

/*
 * This function is use to switch a Panel or FormPanel
 * from one panel to another panel.
 * Parameter Details: (1) 'whichPanel' is the panel which has to be switched.
 * 					  (2) 'toPanel' is the new panel where we have to add the 'whichPanel'
 */
function switchForm(whichPanel, toPanel){
	toPanel.add(whichPanel);
	toPanel.doLayout();
}